package oop.core.view.listeners;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;

import oop.core.Game;
import oop.core.GameLoop;
import oop.core.RunGame;
import oop.core.Scheduler;
import oop.core.ai.CivilianAI;
import oop.core.control.Controller;
import oop.core.entity.EntityLibrary;
import oop.core.entity.MerchantNPC;
import oop.core.entity.OccupationLibrary;
import oop.core.entity.Avatar;
import oop.core.entity.DerivedStats;
import oop.core.entity.Occupation;
import oop.core.entity.Stats;
import oop.core.item.Equipment;
import oop.core.item.Inventory;
import oop.core.item.SellableItem;
import oop.core.item.UsableItem;
import oop.core.map.GameMap;
import oop.core.map.Location;
import oop.core.map.MovementType;
import oop.core.med.AvatarMovementMediator;
import oop.core.med.InteractionItemMediator;
import oop.core.med.InteractionMediator;
import oop.core.med.ItemPickupMediator;
import oop.core.med.MovementLogic;
import oop.core.med.TransactionMediator;
import oop.core.skill.Cone;
import oop.core.skill.Skill;
import oop.core.view.AreaPanel;
import oop.core.view.CharacterCreationMenu;
import oop.core.view.EquipmentPanel;
import oop.core.view.GamePanel;
import oop.core.view.ImageHandler;
import oop.core.view.InventoryPanel;
import oop.core.view.SizeablePanel;
import oop.core.view.SkillsPanel;
import oop.core.view.StatsPanel;

/**
 * Activated upon the user selecting to start a new game in the mainmenu.
 * @author Alex
 *
 */
public class NewGameListener implements ActionListener {

	private GamePanel gamePanel;
	
	private Controller controller;
	private InventoryMouseListener inventoryMouseListener;
	private EquipmentMouseListener equipmentMouseListener;
	private ActiveSkillMouseListener activeSkillMouseListener;
	
	private List<SizeablePanel> panels;

	private AreaPanel areaPanel;
	private StatsPanel statsPanel;
	private SkillsPanel skillsPanel;
	private InventoryPanel inventoryPanel;
	private EquipmentPanel equipmentPanel;
	
	/**
	 * Takes all the information needed to start the game.
	 * @param gamePanel
	 * @param areaPanel
	 * @param statsPanel
	 * @param skillsPanel
	 * @param inventoryPanel
	 * @param equipmentPanel
	 * @param controller
	 * @param inventoryMouseListener
	 * @param equipmentMouseListener
	 * @param activeSkillMouseListener
	 */
	// One hell of a constructor to start a game.
	public NewGameListener(GamePanel gamePanel, AreaPanel areaPanel, StatsPanel statsPanel, SkillsPanel skillsPanel, InventoryPanel inventoryPanel,
							EquipmentPanel equipmentPanel, Controller controller,InventoryMouseListener inventoryMouseListener,
							EquipmentMouseListener equipmentMouseListener, ActiveSkillMouseListener activeSkillMouseListener) {		
		this.controller = controller;
		this.inventoryMouseListener = inventoryMouseListener;
		this.equipmentMouseListener = equipmentMouseListener;
		this.activeSkillMouseListener = activeSkillMouseListener;
		
		this.areaPanel = areaPanel;
		this.statsPanel = statsPanel;
		this.skillsPanel = skillsPanel;
		this.inventoryPanel = inventoryPanel;
		this.equipmentPanel = equipmentPanel;

		panels = new ArrayList<SizeablePanel>();
		panels.add(areaPanel);
		panels.add(statsPanel);
		panels.add(skillsPanel);
		panels.add(inventoryPanel);
		panels.add(equipmentPanel);
		
		this.gamePanel = gamePanel;
	}
	
	public void actionPerformed(ActionEvent e) {
		
		JButton button = (JButton)(e.getSource());
		String name = button.getText();

		//skills.add(new Cone(true, "Fireball", 1, 1000, 3, 5.0f, "foo", 500, 2));
		
		String occupationName;
		DerivedStats stats;
		List<MovementType> movementTypes = new ArrayList<MovementType>();
		
		if(name.equals(CharacterCreationMenu.NEW_SMASHER))
		{
			occupationName = "Smasher";
			stats = new DerivedStats(0, 5, 100, 100, 3, 2, 1, 3, 4250L);
			movementTypes.add(MovementType.swim);
		}
		else if(name.equals(CharacterCreationMenu.NEW_SUMMONER))
		{
			occupationName = "Summoner";
			stats = new DerivedStats(0, 5, 100, 100, 1, 2, 3, 2, 500L);
			movementTypes.add(MovementType.fly);
		}
		else if(name.equals(CharacterCreationMenu.NEW_SNEAK))
		{
			occupationName = "Sneak";
			stats = new DerivedStats(0, 5, 100, 100, 2, 3, 1, 2, 50L);
		}
		else
		{
			occupationName = "Dave";
			stats = new DerivedStats(0, 10, 200, 200, 5, 5, 5, 5, 250L);
			movementTypes.add(MovementType.swim);
			movementTypes.add(MovementType.fly);
		}
		
		movementTypes.add(MovementType.walk);
		
		Occupation occupation = OccupationLibrary.getFromLibrary(occupationName);
		Avatar avatar = new Avatar(0, occupation, occupationName, occupationName, 8, new Location(30, 30), true,
										new Equipment(), new Inventory(), movementTypes, stats);
		System.out.println(avatar.getImageID());
		System.out.println((ImageHandler.getImage(avatar.getImageID()) == null));
		GameMap gameMap = new GameMap();
		
		gameMap.setPlayer(avatar);
//		MerchantNPC merchant = (MerchantNPC) EntityLibrary.getFromLibrary("merchant");
		MerchantNPC merchant = (MerchantNPC)EntityLibrary.getFromLibrary("merchant");
		List<SellableItem> list = new ArrayList<SellableItem>();
		list.add(new UsableItem("Foo", "fire", 10, new Stats(0,0,0,0,10,10,10,10,10)));
		merchant.setInventory(new Inventory());
		merchant.addToInventory(list.get(0));
		merchant.setLoc(new Location(32,32));
		gameMap.addEntity(merchant);
		
		areaPanel.takeMap(gameMap);
		statsPanel.takeStats(avatar.getDerStats());
		skillsPanel.takeOccupation(avatar.getOcc());
		inventoryPanel.takeInventory(avatar.getInventory());
		equipmentPanel.takeEquipment(avatar.getEquipment());
		
		Game game = new Game(avatar, gameMap, panels, controller, inventoryMouseListener, equipmentMouseListener, activeSkillMouseListener);
	}

}
